package spring.yongongsystem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import spring.yongongsystem.POJO.dto.MerchantDTO;
import spring.yongongsystem.POJO.dto.WorkerDTO;
import spring.yongongsystem.POJO.dto.WorkerPageQueryDTO;
import spring.yongongsystem.POJO.entity.Admin;
import spring.yongongsystem.POJO.entity.Merchant;
import spring.yongongsystem.POJO.entity.Order;
import spring.yongongsystem.POJO.entity.Worker;
import spring.yongongsystem.exception.AccountNotFoundException;
import spring.yongongsystem.exception.PasswordErrorException;
import spring.yongongsystem.mapper.OrderMapper;
import spring.yongongsystem.mapper.WorkerMapper;
import spring.yongongsystem.result.PageResult;
import spring.yongongsystem.service.WorkerService;

import java.util.List;

@Service
public class WorkerServiceImpl implements WorkerService {

    @Autowired
    private WorkerMapper workerMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Worker login(WorkerDTO workerDTO) {
        String username = workerDTO.getUsername();
        String password = workerDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Worker worker = workerMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (worker == null) {
            //账号不存在
            throw new AccountNotFoundException("帐号不存在");
        }

        //密码比对
        //对前端传过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(worker.getPassword())) {
            //密码错误
            throw new PasswordErrorException("密码错误");
        }
        //3、返回实体对象
        return worker;
    }

    @Override
    public void save(WorkerDTO workerDTO) {
        Worker worker = new Worker();
        BeanUtils.copyProperties(workerDTO,worker);
        worker.setPassword(DigestUtils.md5DigestAsHex(worker.getPassword().getBytes()));
        workerMapper.insert(worker);
    }

    @Override
    public PageResult pageQuery(WorkerPageQueryDTO workerPageQueryDTO) {
        PageHelper.startPage(workerPageQueryDTO.getPage(), workerPageQueryDTO.getPageSize());

        Page<Worker> page = workerMapper.pageQuery(workerPageQueryDTO);

        long total = workerMapper.count();
        List<Worker> records = page.getResult();

        return new PageResult(total, records);
    }

    @Override
    public void deleteById(Long id) {
        workerMapper.deleteById(id);
    }

    @Override
    public void update(WorkerDTO workerDTO) {
        Worker worker = new Worker();
        BeanUtils.copyProperties(workerDTO,worker);
        worker.setPassword(DigestUtils.md5DigestAsHex(worker.getPassword().getBytes()));
        workerMapper.update(worker);
    }

    @Override
    public void saveGroup(Long id,Long groupId) {
        Worker worker = workerMapper.getById(id);
        if (worker != null) { // 检查 worker 是否为 null
            worker.setGroupId(groupId);
            workerMapper.update(worker);
        } else {
            // 处理 worker 为 null 的情况，例如抛出异常或记录日志
            throw new IllegalArgumentException("Worker with id " + id + " not found");
        }
    }

    @Override
    public void backGroup(Long id) {
        Worker worker = workerMapper.getById(id);
        if (worker != null) { // 检查 worker 是否为 null
            worker.setGroupId(null);
            workerMapper.update(worker);
        } else {
            // 处理 worker 为 null 的情况，例如抛出异常或记录日志
            throw new IllegalArgumentException("Worker with id " + id + " not found");
        }
    }

    @Override
    public void accept(Long workerId, Long orderId) {
        Order order = orderMapper.getById(orderId);
        order.setWorkerId(workerId);
        order.setOrderStatus("进行中");
        orderMapper.update(order);
    }

    @Override
    public void success(Long id,String photo) {
        Order order = orderMapper.getById(id);
        order.setOrderStatus("已完成");
        order.setPhoto(photo);
        orderMapper.update(order);
    }
}
